package com.vil.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vil.common.ServerResponse;

import com.vil.entity.Cars;
import com.vil.mapper.CarsMapper;

import com.vil.service.CarsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CarsServiceImpl implements CarsService {
    @Autowired
    private CarsMapper mapper;

    @Override
    public ServerResponse deleteByPrimaryKey(Long ids) {

        try {
            int i = mapper.deleteByPrimaryKey(ids);

            if (i > 0) {
                return ServerResponse.buildSuccessMsg("删除成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ServerResponse.buildErrorMsg("删除失败");
    }


    @Override
    public ServerResponse insertSelective(Cars cars) {
        cars.setCreate_time(new Date());

        try {
            int i = mapper.insertSelective(cars);
            if (i > 0) {
                return ServerResponse.buildSuccessMsg("新增成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ServerResponse.buildErrorMsg("新增失败");
    }


    @Override
    public ServerResponse updateByPrimaryKeySelective(Cars record) {

        try {
            int i = mapper.updateByPrimaryKeySelective(record);
            if (i > 0) {
                return ServerResponse.buildSuccessMsg("修改成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ServerResponse.buildErrorMsg("修改失败");
    }


    @Override
    public ServerResponse getListByManyConditions(Integer pageIndex, Integer pageSize, Cars cars) {
        PageHelper.startPage(pageIndex, pageSize);
        List<Cars> list = null;
        try {
            list = mapper.getListByManyConditions(cars);
        } catch (Exception e) {
            e.printStackTrace();
        }
        PageInfo carsPageInfo = new PageInfo(list);
        return ServerResponse.buildSuccessData(carsPageInfo);
    }


    @Override
    public ServerResponse deleteMany(Long[] ids) {
        try {
            if (ids==null){
                return ServerResponse.buildErrorMsg("数组不能为空");
            }
            int i = 0;
            i = mapper.deleteMany(ids);
            if (i>0){
                return ServerResponse.buildSuccessMsg("删除成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ServerResponse.buildErrorMsg("删除失败");
    }
    @Override
    public ServerResponse getById(Long id) {
       Cars cars= null;
        try {
            cars = mapper.selectByPrimaryKey(id);
            if (cars==null){
                return ServerResponse.buildErrorMsg("不存在该管理员信息");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ServerResponse.buildSuccessData(cars);
    }
}

